package com.wawayaya.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import de.greenrobot.event.EventBus;
import wawayaya.kids_iread.R;
import com.wawayaya.base.BaseFragment;
import com.wawayaya.bean.BookShelf;
import com.wawayaya.bean.Books;
import com.wawayaya.bean.HomePage;
import com.wawayaya.db.bookshelf.RequestThread;
import com.wawayaya.db.entitydao.BookShelfEntityDao;
import com.wawayaya.ui.MainActivity;
import com.wawayaya.ui.widget.BookContiner;
import com.wawayaya.util.TDevice;
import com.wawayaya.util.TLog;
import com.wawayaya.util.event.ShelfEvent;

import java.util.List;

/**
 * 首页
 * @author
 * @version 1.0
 * @created 2015-11-3
 */
public class HomeFragment extends BaseFragment   {

    private MainActivity       mainActivity;

    private RequestThread requestThread;
    private boolean  isZh;
    private BookContiner  bookContiner;
    private RelativeLayout   rlprogressBa;
    private ScrollView       svBookShelf;

    public  HomeFragment(){
    }

    public HomeFragment(RequestThread requestThread){
        this.requestThread = requestThread;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mainActivity = (MainActivity)context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_home, null);
        initView(view);
        initData();
        onClick(view);
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onClick(View v) {


    }

    @Override
    public void initView(View view) {
        bookContiner =  (BookContiner)view.findViewById(R.id.bc_home_continer);
        rlprogressBa = (RelativeLayout)view.findViewById(R.id.rl_loading_bar);
        svBookShelf = (ScrollView) view.findViewById(R.id.sv_book_shelf);
    }

    @Override
    public void initData() {
        this.isZh = TDevice.isZhCN();

        //TLog.log("homepage....."+homePages.size());
    }

    @Override
    public void clear() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * event回调 刷新titleBar
     * @param event
     */
    @Override
    public void onEvent(ShelfEvent event) {
        TLog.log("event = " + event.eventType);
        if (event.eventType == ShelfEvent.refreshIndexData) {
            TLog.log("HomePageMent refresh event = " + event.eventType);
            rlprogressBa.setVisibility(View.GONE);
            loadShelfData();
        }

    }

    /**
     * 加载书架数据
     */
    private void  loadShelfData(){
        if(null==requestThread){
            requestThread =mainActivity.getRequestData();
        }
        //home page data
        List<HomePage>  homePages=  requestThread.getSaveDB().getHomepageData();
        if(homePages.size()>0){
            svBookShelf.setVisibility(View.VISIBLE);
        }
        TLog.log("HomePageMent refresh size = " + homePages.size());
        bookContiner.initData(isZh);
        bookContiner.showView(homePages);
    }
}
